<!-- src/views/SafetyHelmet.vue -->
<template>

  <div class="detail-page">
    <!-- 标题与简介 -->
    <div class="banner-section">
      <div class="header-content">
        <h1>{{ algorithmInfo.name }}</h1>
        <p>
          {{ algorithmInfo.description }}
        </p>
        <button class="apply-btn" @click="toApplyDetail">试用</button>
      </div>
      <div class="video-preview">
        <div id="img" style="width: 100%">
          <img :src="algorithmInfo.headImageUrl" alt="视频预览"/>
          <button class="play-btn" @click="imgToVideo" style="left: 38%">▶</button>
        </div>
        <div id="video" style="display: none">
          <video :src="algorithmInfo.videoUrl" type="video/mp4" controls autoplay muted/>
        </div>
      </div>
    </div>
    <!-- 主体内容：左右布局 -->
    <div class="content-wrapper">
      <!-- 左侧内容 -->
      <div class="left-content">
        <h3>应用场景</h3>
        <ul class="app-list" style="list-style-type: disc; margin-left: 20px;">
          <li v-for="(itme, id) in algorithmInfo.applyScenarios" :key="id">{{ itme }}</li>
        </ul>
        <h3>常见问题</h3>
        <ul class="faq-list" style="list-style-type: disc; margin-left: 20px;">
          <li v-for="(itme, id) in algorithmInfo.commonQuestions" :key="id">{{ itme }}</li>
        </ul>
      </div>
    </div>

    <!-- 相关算法推荐 -->
    <div class="related-algorithms">
      <h3>相关算法</h3>
      <div class="algorithm-grid">
        <div class="algo-card" @click="togetRelatedAlgorithms(item.id)"
             v-for="(item, index) in relatedAlgorithms" :key="index">
          <div class="algo-img">
            <img :src="item.imageUrl" alt=""/>
            <span class="tag">{{ item.tag }}</span>
          </div>
          <div class="algo-info">
            <img :src="item.iconUrl" alt="icon" class="info-icon"/>
            <h4>{{ item.title }}</h4>
            <p>{{ item.desc }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 返回按钮 -->
    <div class="back-button">
      <button @click="$router.back()">返回</button>
    </div>
  </div>
</template>

<script>
import {algorithmInfoGetVoUsingGet, algorithmInfoRecommendUsingGet} from '@/api/front/apIsuanfa';

export default {
  name: 'AlgorithmInfo',
  data() {
    return {
      algorithmInfo: {},
      relatedAlgorithms: [],
    }
  },
  methods: {
    imgToVideo() {
      const imgDom = document.querySelector('#img');
      const videoDom = document.querySelector('#video');


      // 获取图片的实际宽度和高度
      const imgWidth = imgDom.firstChild.clientWidth;
      const imgHeight = imgDom.firstChild.clientHeight;

      // 隐藏图片
      imgDom.style.display = 'none';


      // 将宽度和高度设置给视频元素
      videoDom.firstChild.width = imgWidth;
      videoDom.firstChild.height = imgHeight;

      // 显示视频元素
      videoDom.style.display = 'block';
    },
    toApplyDetail() {
      this.$router.push({path: `/algorithmApplyDetail/${this.$route.params.id}`});
    },
    getAlgorithmInfo() {
      const algorithmId = this.$route.params.id;
      // 根据 algorithmId 调用接口获取算法详情数据
      // 这里可以使用 axios 或其他 HTTP 库进行 API 请求
      console.log('获取算法详情，ID:', algorithmId);
      algorithmInfoGetVoUsingGet({
        params: {
          id: algorithmId
        }
      }).then(res => {
        if (res.success === true) {
          this.algorithmInfo = res.data;
        }
      }).catch(error => {
        console.error('获取算法详情失败:', error);
      });
    },
    getRelatedAlgorithms() {
      algorithmInfoRecommendUsingGet({
        params: {
          algorithmId: this.$route.params.id
        }
      }).then(res => {
        if (res.success === true) {
          // 根据实际返回数据结构调整
          this.relatedAlgorithms = res.data.map(item => ({
            id: item.algorithmId,
            title: item.name,
            desc: item.description,
            tag: item.tag || '算法',
            imageUrl: item.headImageUrl,
            iconUrl: item.logo
          }));
          // console.log(this.relatedAlgorithms)
        }
      }).catch(error => {
        console.error('获取相关算法失败:', error);
      })
    },
    togetRelatedAlgorithms(id) {

      this.$router.push({path: `/algorithmInfo/${id}`});
    },

    fetchData() {
      this.getAlgorithmInfo();
      this.getRelatedAlgorithms();
    },
  },
  mounted() {
    // 获取算法详情数据
    this.getAlgorithmInfo();
    this.getRelatedAlgorithms();
    this.fetchData();
  },
  watch: {
    // 监听路由变化，当路由参数改变时重新获取数据
    '$route'(to, from) {
      // 当路由参数 id 发生变化时，重新获取数据
      if (to.params.id !== from.params.id) {
        this.fetchData();
      }
    }
  }

};
</script>

<style scoped>
.detail-page {
  padding: 0;
  max-width: 1500px;
  margin: 0;
  font-family: MiSans,
  Avenir,
  Helvetica,
  Arial,
  sans-serif;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}


.banner-section {
  padding: 40px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #00AF84;
  color: white;
  border-radius: 8px;
  margin-bottom: 40px;
  height: 270px;
  width: 1450px;
  position: relative;
}

.header-content {
  text-align: left;
  flex: 1;
  padding-left: 100px;
}

.header-content h1 {
  font-size: 45px;
  color: #000;
  margin-bottom: 16px;
  font-weight: 500;
}

.header-content p {
  color: #666;
  line-height: 1.6;
  margin-bottom: 20px;
  width: 700px;
}

.apply-btn {
  background-color: #00AF84;
  color: white;
  border: none;
  padding: 10px 24px;
  border-radius: 0px;
  cursor: pointer;
  font-size: 14px;
  width: 150px;
  margin-left: 0px;
}

.apply-btn:hover {
  background-color: #009670;
}

.video-preview {
  flex: 1;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
}

.video-preview img {
  width: 75%;
  height: 75%;
  object-fit: cover;
  border-radius: 8px;
  /* //margin-left: 80px; */
}

.play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border: none;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding-bottom: 5px;
}

.content-wrapper {
  display: flex;
  gap: 60px;
  margin-bottom: 0px;
}

.left-content {
  flex: 2;
  min-width: 500px;
  margin-left: 100px;
}

.left-content h3 {
  font-size: 20px;
  color: #333;
  margin-top: 0;
  margin-bottom: 16px;
}

.app-list,
.faq-list {
  list-style: none;
  padding-left: 0;
}

.app-list li,
.faq-list li {
  margin-bottom: 12px;
  color: #555;
  line-height: 1.6;
  font-size: 14px;
}


.play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border: none;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bottom-content {
  display: flex;
}

.related-algorithms {
  margin-bottom: 0px;
}

.related-algorithms h3 {
  font-size: 20px;
  color: #333;
  margin-bottom: 20px;
  margin-left: 95px;
}

.algorithm-grid {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  width: 100%;
  margin-left: 95px;
  margin-bottom: 20px;
}

.algo-card {
  border: 2px solid #eee;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s ease;
  width: 400px;
}

.algo-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.algo-img {
  position: relative;
  width: 100%;
  height: 140px;
  overflow: hidden;
  margin: 0 auto;
}

.algo-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.algo-info {
  padding: 0px;
  width: 180px;
}

/* 卡片图标 */
.info-icon {
  width: 50px;
  height: 50px;
  margin-right: 8px;
  margin-left: 30px;
  position: relative;
  /*使用绝对定位不受父元素影响*/
  top: 40px;
  /* 向下移动 10px */
}

.algo-img .tag {
  position: absolute;
  bottom: 0px;
  /* 距离底部 8px */
  left: 0px;
  /* 距离左边 8px */
  background-color: #00AF84;
  color: white;
  padding: 4px 8px;
  font-size: 12px;
  z-index: 10;
}

.algo-info .tag {
  display: inline-block;
  background-color: #00AF84;
  color: white;
  padding: 4px 8px;
  font-size: 12px;
  margin-bottom: 8px;
}

.algo-info h4 {
  font-size: 16px;
  color: #333;
  padding-left: 100px;
  margin-top: 0px;
  position: relative;
  bottom: 20px;
}

.algo-info p {
  font-size: 14px;
  color: #666;
  margin: 0;
  width: 225px;
  padding-left: 100px;
  padding-bottom: 10px;
  position: relative;
  bottom: 20px;

}

.back-button {
  text-align: center;
  margin-top: 10px;
  padding-bottom: 20px;

}

.back-button button {
  background-color: #00AF84;
  color: white;
  border: none;
  padding: 10px 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.back-button button:hover {
  background-color: #009670;
}
</style>
